/*
PDFCS_DEVICEGRAY		1	
PDFCS_DEVICERGB			2
PDFCS_DEVICECMYK		3
PDFCS_CALGRAY			4
PDFCS_CALRGB			5
PDFCS_LAB				6
PDFCS_ICCBASED			7
PDFCS_SEPARATION		8
PDFCS_DEVICEN			9
PDFCS_INDEXED			10
PDFCS_PATTERN			11
*/

function ColorConvertor() {

}


ColorConvertor.prototype.GetColorValueString = function(colorspacetype, colorcomponents, encodeArr, rangeArr) {
	switch (colorspacetype)
	{
	case 1: // DeviceGray
		return ColorConvertor.prototype.GetSampleRGBValueString(colorspacetype, colorcomponents, encodeArr, rangeArr);
	break;
	case 2: // DeviceRGB
		return ColorConvertor.prototype.GetSampleRGBValueString(colorspacetype, colorcomponents, encodeArr, rangeArr);
	break;
	case 3: // DeviceCMYK
		return ColorConvertor.prototype.GetSampleRGBValueString(colorspacetype, colorcomponents, encodeArr, rangeArr);
	break;
	case 4: // CalGray
	break;
	case 5: // CalRGB
	break;
	case 6: // Lab
	break;
	case 7: // IccBased
	break;
	case 8: // Separation
	break;
	case 9: // DeviceN
	break;
	case 10: // Indexed
	break;
	case 11: // Pattern
	break;
	}
	return "RGB(0, 0, 0)";
}


ColorConvertor.prototype.GetRGBValueStringFromFloat = function(colorspacetype, colorcomponents) {
	var R=0, G=0, B=0;
	if (1 === colorspacetype){ // DeviceGray
		var grayVal = colorcomponents[0];
		R = Math.round(colorcomponents[0]*255);
		G = R;
		B = R;
	}
	else if(2 === colorspacetype){ // DeviceRGB
		var fr = colorcomponents[0];
		var fg = colorcomponents[1];
		var fb = colorcomponents[2];
		
		R = Math.round(fr * 255); 
		G = Math.round(fg * 255);
		B = Math.round(fb * 255);
	}
	else if(3 === colorspacetype){ // DeviceCMYK
		var fc = colorcomponents[0];
		var fm = colorcomponents[1];
		var fy = colorcomponents[2];
		var fk = colorcomponents[3];
		
		var fR = 1.0 - (fc + fk);
		if (fR < 0) fR = 0;
		var fG = 1.0 - (fm + fk);
		if (fG < 0) fG = 0;
		var fB = 1.0 - (fy + fk);
		if (fB < 0) fB = 0;
		R = Math.round(fR*255);
		G = Math.round(fG*255);
		B = Math.round(fB*255);
	}

	return "RGB("+R+","+G+","+B+")";
}


ColorConvertor.prototype.GetSampleRGBValueString = function(colorspacetype, colorcomponents, encodeArr, rangeArr) {

	var R=0, G=0, B=0;
	if (1 === colorspacetype){ // DeviceGray
		var grayVal = colorcomponents[0]/encodeArr[1];
		if (grayVal < rangeArr[0]) grayVal = grangeArr[0]; if (grayVal > rangeArr[1]) grayVal = grayValArr[1];
		R = Math.round(colorcomponents[0]*255);
		G = R;
		B = R;
	}
	else if(2 === colorspacetype){ // DeviceRGB
		var fr = colorcomponents[0]/encodeArr[1];
		var fg = colorcomponents[1]/encodeArr[1];
		var fb = colorcomponents[2]/encodeArr[1];
		
		R = Math.round(fr * 255); 
		G = Math.round(fg * 255);
		B = Math.round(fb * 255);
	}
	else if(3 === colorspacetype){ // DeviceCMYK
		var fc = colorcomponents[0]/encodeArr[1];
		var fm = colorcomponents[1]/encodeArr[1];
		var fy = colorcomponents[2]/encodeArr[1];
		var fk = colorcomponents[3]/encodeArr[1];
		
		var fR = 1.0 - (fc + fk);
		if (fR < 0) fR = 0;
		var fG = 1.0 - (fm + fk);
		if (fG < 0) fG = 0;
		var fB = 1.0 - (fy + fk);
		if (fB < 0) fB = 0;
		R = Math.round(fR*255);
		G = Math.round(fG*255);
		B = Math.round(fB*255);
	}

	return "RGB("+R+","+G+","+B+")";
}

ColorConvertor.prototype.GetSampleRGBValueArray = function(colorspacetype, colorcomponents, encodeArr, rangeArr) {
	var R=0, G=0, B=0;
	
	if (1 === colorspacetype) { // DeviceGray
		var grayVal = colorcomponents[0]/encodeArr[1];
		if (grayVal < rangeArr[0]) grayVal = grangeArr[0];
		if (grayVal > rangeArr[1]) grayVal = grayValArr[1];
		R = Math.round(grayVal*255);
		G = R;
		B = R;
	}
	else if (2 === colorspacetype) { // DeviceRGB
		var fr = colorcomponents[0]/encodeArr[1];
		var fg = colorcomponents[1]/encodeArr[1];
		var fb = colorcomponents[2]/encodeArr[1];
		R = Math.round(fr*255);
		G = Math.round(fg*255);
		B = Math.round(fb*255);
	}
	else if (3 === colorspacetype) { // DeviceCMYK
		var fc = colorcomponents[0]/encodeArr[1];
		var fm = colorcomponents[1]/encodeArr[1];
		var fy = colorcomponents[2]/encodeArr[1];
		var fk = colorcomponents[3]/encodeArr[1];
		var fr = 1.0 - (fc + fk);
		if (fr < 0) fr = 0;
		var fg = 1.0 - (fm + fk);
		if (fg < 0) fg = 0;
		var fb = 1.0 - (fy + fk);
		if (fb < 0) fb = 0;
		R = Math.round(fr*255);
		G = Math.round(fg*255);
		B = Math.round(fb*255);
	}
	
	return [R, G, B];
}
